package munci.read;

import java.io.Reader;

import javacc.InputConstants;
import javacc.InputTokenManager;
import javacc.JavaCharStream;

public class ReaderTokenizer implements Tokenizer {
    
    private final InputTokenManager toks;
    private final String source;
    
    private boolean next = true;
    private Token token;
    
    public ReaderTokenizer(Reader input, String source) {
        toks = new InputTokenManager(new JavaCharStream(input));
        this.source = source;
    }
    
	public void nextToken() {
		if (next) {
			throw new RuntimeException("Current token has not been consumed");
		}
		next = true;
	}

	@Override
	public Token token() {
		if (next) {
	        javacc.Token tok = toks.getNextToken();
	        token = (tok.kind == InputConstants.EOF? null : Token.createToken(tok, source));
	        next = false;
		}
		return token;
	}
}
